﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace System.Utils
{
	internal class WebSiteDeployHelper
	{
#if DEBUG
		private const string StaticFileServer = "dev.static.iwapz.com";
#else
		private const string StaticFileServer = "static.iwapz.com";
#endif

		internal static string ImportStaticFiles(string[] paths)
		{
			//string application = HttpContext.Current.Application.GetRequiredValue<string>("app_static_server_folder");

			string application = string.Empty;

			StringBuilder sb = new StringBuilder();
			foreach (var path in paths)
			{
				var uri = string.Empty;
				#region retrieve absolute uri
				if (path.StartsWith("/"))
					uri = string.Format("http://{0}/{1}", StaticFileServer, path.Remove(0, 1));
				else if (path.StartsWith("$"))
					uri = string.Format("http://{0}/share/{1}", StaticFileServer, path.Remove(0, 1));
				else if (path.StartsWith("~/"))
					uri = string.Format("http://{0}/{1}/{2}", StaticFileServer, application, path.Remove(0, 2));
				#endregion

				var extension = Path.GetExtension(uri);
				if (extension.Equals(".js", StringComparison.CurrentCultureIgnoreCase))
					sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}\" ></script>\n", uri);
				else if (extension.Equals(".css", StringComparison.CurrentCultureIgnoreCase))
					sb.AppendFormat("<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}\" />\n", uri);
			}

			return sb.ToString();
		}
	}
}
